home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 021a / hpgl2scr.zip / HPGL2SCR.C next >
Text File  |  1991-09-19  |  10KB  |  350 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*  Converts HPGL files to AutoCAD script files.  HPGL file must have       */
  4. /*  extension ".PLT", and script file will have extension ".SCR".  To       */
  5. /*  find out more information, consult the "HP ColorPro Graphics Plotter    */
  6. /*  Programming Manual" available from HP, your AutoCad manual, and "Using  */
  7. /*  AutoCAD, 3rd Edition" by QUE Corporation.                               */
  8. /*                                                                          */
  9. /*  This program copyright (c) 1991 by William D. Palmer, 1313 Vickers Ave. */
  10. /*  Durham, NC  27707.  The program is released into the public domain,     */
  11. /*  and any commercial use without the prior written consent of the         */
  12. /*  copyright holder is prohibited.  No warranty as to the suitability of   */
  13. /*  this software for any particular purpose is expressed or implied.       */
  14. /*                                                                          */
  15. /*  HPGL2SCR.C                                                   12 SEP 91  */
  16. /*                                                                          */
  17. /****************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #undef  DEBUG
  24. #define BUFFSIZE 12800
  25. #define DOWN 0x01
  26. #define UP   0x00
  27.  
  28. char pen = UP, *ptr, *endptr, buffer[BUFFSIZE],
  29.      infile_line[80], infile_name[80], outfile_name[80];
  30.  
  31. double AX, AY, height, M1X, M1Y, M2X, M2Y, width, X, Y, params[20];
  32.  
  33. FILE *infile, *outfile;
  34.  
  35. int P1X = 250, P1Y = 279, P2X = 10250, P2Y = 7479,
  36.     U1X = 250, U1Y = 279, U2X = 10250, U2Y = 7479,
  37.     X_RANGE = 10000, Y_RANGE = 7200,
  38.     characters, index, jndex, kndex;
  39.  
  40. void init_eq (void)
  41.   {
  42.   M1X = (P2X - P1X) / (U2X - U1X);
  43.   M2X = P1X - U1X * M1X;
  44.   M1Y = (P2Y - P1Y) / (U2Y - U1Y);
  45.   M2Y = P1Y - U1Y * M1Y;
  46.   }
  47.  
  48. void get_coords (void)
  49.   {
  50.   X = (M1X * AX + M2X) / 1000;
  51.   Y = (M1Y * AY + M2Y) / 1000;
  52.   }
  53.  
  54. void main (int argc, char *argv[])
  55.   {
  56. /* Link floating point library; bug in Turbo C 2.0 */
  57.   X = exp (1);
  58.  
  59. /* Open requisite files. */
  60.   if (argc == 1)
  61.     {
  62.     printf ("File To Filter   : ");
  63.     scanf ("%s", &infile_name);
  64.     }
  65.   else strcpy (infile_name, argv[1]);
  66.   if (strrchr (infile_name, '.') == NULL)
  67.     {
  68.     strcpy (outfile_name, infile_name);
  69.     strcat (infile_name, ".PLT");
  70.     strcat (outfile_name, ".SCR");
  71.     }
  72.   else
  73.     {
  74.     index = 0;
  75.     while (infile_name[index] != '.')
  76.       outfile_name[index] = infile_name[index++];
  77.     strcat (outfile_name, ".SCR");
  78.     }
  79.   infile = fopen (infile_name, "r");
  80.   outfile = fopen (outfile_name, "w");
  81.  
  82. #ifdef DEBUG
  83.   printf ("%s, %s\n", infile_name, outfile_name);
  84.   printf ("%p, %p\n", infile, outfile);
  85.   (void) getch ();
  86. #endif DEBUG
  87.  
  88. /* Initialize scaling equation. */
  89.   init_eq ();
  90.   strcpy (infile_line, "");
  91.   jndex = 0;
  92.  
  93. /* Process the input file until done. */
  94.   while (!feof (infile))
  95.     {
  96.     index = 0;
  97.     characters = fread (buffer, 1, BUFFSIZE, infile);
  98. #ifdef DEBUG
  99.   printf ("characters = %d\n", characters);
  100.   (void) getch ();
  101. #endif DEBUG
  102.     while (index < characters)
  103.       {
  104.       if ((buffer[index] != 0x0d) && (buffer[index] != 0x0a))
  105.     infile_line[jndex++] = buffer[index];
  106.       if (buffer[index++] == ';')
  107.     {
  108. #ifdef DEBUG
  109.   printf ("\n");
  110. #endif DEBUG
  111.     printf ("#");
  112.     infile_line[jndex] = 0x00;
  113.  
  114. /* Here's where the HPGL commands get processed into AUTOCAD script. */
  115. #ifdef DEBUG
  116.   printf ("%s\n", infile_line);
  117. #endif DEBUG
  118.  
  119. /* Process LB instruction.  If it's an LB, other command strings could  */
  120. /* appear in the message line.                                          */
  121.       if ((ptr = strstr (infile_line, "LB")) != NULL)
  122.     {
  123. #ifdef DEBUG
  124.   printf ("Processing LB instruction\n");
  125. #endif DEBUG
  126.     fprintf (outfile ,"TEXT %lf,%lf   ", X, Y);
  127.     kndex = 2;
  128.     while (infile_line[kndex] != 0x03)
  129.       fprintf (outfile, "%c", infile_line[kndex++]);
  130.     fprintf (outfile, "\n");
  131.     } /* if LB */
  132.       else
  133.     {
  134.  
  135. /* Process IP instruction. */
  136.       if ((ptr = strstr (infile_line, "IP")) != NULL)
  137.     {
  138. #ifdef DEBUG
  139.   printf ("Processing IP instruction\n");
  140. #endif DEBUG
  141.     kndex = 0;
  142.     endptr = ptr += 2;
  143.     while (strchr (";ABCDILPRSU", (int) *endptr) == NULL)
  144.       {
  145.       params[kndex] = strtod (ptr, &endptr);
  146.       ptr = endptr + 1;
  147. #ifdef DEBUG
  148.   printf ("%lf ", params[kndex]);
  149.   printf ("%i\n", kndex);
  150. #endif DEBUG
  151.       kndex++;
  152.       } /* while */
  153.     switch (kndex)
  154.       {
  155.       case 0 : P1X = 250; P1Y = 279; P2X = 10250; P2Y = 7479;
  156.            X_RANGE = 10000; Y_RANGE = 7200; init_eq (); break;
  157.       case 1 : break;
  158.       case 2 : P1X = (int) params[0]; P1Y = (int) params[1];
  159.            P2X = P1X + X_RANGE; P2Y = P1Y + Y_RANGE;
  160.            init_eq (); break;
  161.       case 3 : break;
  162.       case 4 :
  163.       default: P1X = (int) params[0]; P1Y = (int) params[1];
  164.            P2X = (int) params[2]; P2Y = (int) params[3];
  165.            X_RANGE = P2X - P1X; Y_RANGE = P2Y - P1Y;
  166.            init_eq (); break;
  167.  
  168.       } /* switch */
  169. #ifdef DEBUG
  170.   (void) getch ();
  171. #endif DEBUG
  172.     } /* if IP */
  173.  
  174. /* Process SC instruction. */
  175.       if ((ptr = strstr (infile_line, "SC")) != NULL)
  176.     {
  177. #ifdef DEBUG
  178.   printf ("Processing SC instruction\n");
  179. #endif DEBUG
  180.     kndex = 0;
  181.     endptr = ptr += 2;
  182.     while (strchr (";ABCDILPRSU", (int) *endptr) == NULL)
  183.       {
  184.       params[kndex] = strtod (ptr, &endptr);
  185.       ptr = endptr + 1;
  186. #ifdef DEBUG
  187.   printf ("%lf ", params[kndex]);
  188.   printf ("%i\n", kndex);
  189. #endif DEBUG
  190.       kndex++;
  191.       } /* while */
  192.     switch (kndex)
  193.       {
  194.       case 0 : U1X = P1X; U1Y = P1Y; U2X = P2X; U2Y = P2Y;
  195.            init_eq (); break;
  196.       case 1 :
  197.       case 2 :
  198.       case 3 : break;
  199.       case 4 :
  200.       default: U1X = (int) params[0]; U2X = (int) params[1];
  201.            U1Y = (int) params[2]; U2Y = (int) params[3];
  202.            if ((U1X == U2X) || (U1Y == U2Y))
  203.              {
  204.              U1X = P1X; U1Y = P1Y; U2X = P2X; U2Y = P2Y;
  205.              }
  206.            init_eq (); break;
  207.       } /* switch */
  208.     } /* if SC */
  209.  
  210. /* Process SR instruction. */
  211.       if ((ptr = strstr (infile_line, "SR")) != NULL)
  212.     {
  213. #ifdef DEBUG
  214.   printf ("Processing SR instruction\n");
  215. #endif DEBUG
  216.     kndex = 0;
  217.     endptr = ptr += 2;
  218.     while (strchr (";ABCDILPRSU", (int) *endptr) == NULL)
  219.       {
  220.       params[kndex] = strtod (ptr, &endptr);
  221.       ptr = endptr + 1;
  222. #ifdef DEBUG
  223.   printf ("%lf ", params[kndex]);
  224.   printf ("%i\n", kndex);
  225. #endif DEBUG
  226.       kndex++;
  227.       } /* while */
  228.     switch (kndex)
  229.       {
  230.       case 1 :
  231.       case 2 : width  = X_RANGE * 0.0075 / 1000.0;
  232.            height = Y_RANGE * 0.0150 / 1000.0;
  233.            break;
  234.       case 3 :
  235.       default: width  = params[0] / 100000.0 * X_RANGE;
  236.            height = params[1] / 100000.0 * Y_RANGE;
  237.            break;
  238.       } /* switch */
  239.     if (pen) fprintf (outfile, "\n");
  240.     fprintf (outfile, "STYLE  TXT %lf %lf    ", height, (width/height));
  241.     if (!pen) fprintf (outfile, "\n");
  242. #ifdef DEBUG
  243.   printf ("STYLE  TXT %lf %lf    #\n", height, (width/height));
  244.   (void) getch ();
  245. #endif DEBUG
  246.     } /* if SR */
  247.  
  248. /* Process PD instruction. */
  249.       if ((ptr = strstr (infile_line, "PD")) != NULL)
  250.     {
  251. #ifdef DEBUG
  252.   printf ("Processing PD instruction\n");
  253. #endif DEBUG
  254.     if (!pen)
  255.       {
  256.       pen = DOWN;
  257.       fprintf (outfile, "LINE %lf,%lf ", X, Y);
  258.       } /* if (!pen) */
  259.     endptr = ptr += 2;
  260.     while (strchr (";ABCDILPRSU", (int) *endptr) == NULL)
  261.       {
  262.       AX = strtod (ptr, &endptr);
  263. #ifdef DEBUG
  264.   printf ("%lf,", AX);
  265. #endif DEBUG
  266.       ptr = endptr + 1;
  267.       AY = strtod (ptr, &endptr);
  268. #ifdef DEBUG
  269.   printf ("%lf -> ", AY);
  270. #endif DEBUG
  271.       ptr = endptr + 1;
  272.       get_coords ();
  273.       fprintf (outfile, "%lf,%lf ", X, Y);
  274. #ifdef DEBUG
  275.   printf ("%lf,%lf\n", X, Y);
  276. #endif DEBUG
  277.       } /* while */
  278.     } /* if PD */
  279.  
  280. /* Process PA instruction. */
  281.       if ((ptr = strstr (infile_line, "PA")) != NULL)
  282.     {
  283. #ifdef DEBUG
  284.   printf ("Processing PA instruction\n");
  285. #endif DEBUG
  286.     endptr = ptr += 2;
  287.     while (strchr (";ABCDILPRSU", (int) *endptr) == NULL)
  288.       {
  289.       AX = strtod (ptr, &endptr);
  290. #ifdef DEBUG
  291.   printf ("%lf,", AX);
  292. #endif DEBUG
  293.       ptr = endptr + 1;
  294.       AY = strtod (ptr, &endptr);
  295. #ifdef DEBUG
  296.   printf ("%lf -> ", AY);
  297. #endif DEBUG
  298.       ptr = endptr + 1;
  299.       get_coords ();
  300.       if (pen) fprintf (outfile, "%lf,%lf ", X, Y);
  301. #ifdef DEBUG
  302.   printf ("%lf,%lf\n", X, Y);
  303. #endif DEBUG
  304.       } /* while */
  305.     } /* if PA */
  306.  
  307. /* Process PU instruction. */
  308.       if ((ptr = strstr (infile_line, "PU")) != NULL)
  309.     {
  310. #ifdef DEBUG
  311.   printf ("Processing PU instruction\n");
  312. #endif DEBUG
  313.     if (pen) fprintf (outfile, "\n");
  314.     pen = UP;
  315.     endptr = ptr += 2;
  316.     while (strchr (";ABCDILPRSU", (int) *endptr) == NULL)
  317.       {
  318.       AX = strtod (ptr, &endptr);
  319. #ifdef DEBUG
  320.   printf ("%lf,", AX);
  321. #endif DEBUG
  322.       ptr = endptr + 1;
  323.       AY = strtod (ptr, &endptr);
  324. #ifdef DEBUG
  325.   printf ("%lf -> ", AY);
  326. #endif DEBUG
  327.       ptr = endptr + 1;
  328. #ifdef DEBUG
  329.   printf ("%lf,%lf\n", X, Y);
  330. #endif DEBUG
  331.       } /* while */
  332.     } /* if PU */
  333.  
  334.     } /* else LB */
  335.  
  336. /* re-initialize infile_line */
  337.     strcpy (infile_line, "");
  338.     jndex = 0;
  339.     } /* if buffer[index] == ';' */
  340.       } /* while (index < characters) */
  341.     } /* while (!feof (infile)) */
  342.   fprintf (outfile, "REDRAW ");
  343.   fclose (infile);
  344.   fclose (outfile);
  345. #ifdef DEBUG
  346.   printf ("infile closed\n");
  347.   printf ("outfile closed\n");
  348. #endif DEBUG
  349.   } /* main */
  350.